{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "---\n",
    "title: '`@parameter`'\n",
    "description: Executes a function or if statement at compile time.\n",
    "css: /static/styles/page-navigation.css\n",
    "website:\n",
    "  open-graph:\n",
    "    image: /static/images/mojo-social-card.png\n",
    "  twitter-card:\n",
    "    image: /static/images/mojo-social-card.png\n",
    "---"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can add the `@parameter` decorator on an `if` statement or on a nested\n",
    "function to run that code at compile time.\n",
    "\n",
    "## Parametric if statement\n",
    "\n",
    "You can add `@parameter` to any `if` condition that's based on a valid\n",
    "parameter expression (it's an expression that evaluates at compile time). This\n",
    "ensures that only the live branch of the `if` statement is compiled into the\n",
    "program, which can reduce your final binary size. For example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this will be included in the binary\n"
     ]
    }
   ],
   "source": [
    "@parameter\n",
    "if True:\n",
    "    print(\"this will be included in the binary\")\n",
    "else:\n",
    "    print(\"this will be eliminated at compile time\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Parametric closure\n",
    "\n",
    "You can add `@parameter` on a nested function to create a “parametric”\n",
    "capturing closure. This means you can create a closure function that captures\n",
    "values from the outer scope (regardless of whether they are variables or\n",
    "parameters), and then use that closure as a parameter. For example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "fn use_closure[func: fn(Int) capturing -> Int](num: Int) -> Int:\n",
    "    return func(num)\n",
    "\n",
    "fn create_closure():\n",
    "    var x = 1\n",
    "\n",
    "    @parameter\n",
    "    fn add(i: Int) -> Int:\n",
    "        return x + i\n",
    "\n",
    "    var y = use_closure[add](2)\n",
    "    print(y)\n",
    "\n",
    "create_closure()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Without the `@parameter` decorator, you'll get a compiler error that says you\n",
    "\"cannot use a dynamic value in call parameter\"—referring to the\n",
    "`use_closure[add](2)` call—because the `add()` closure would still be dynamic."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    ":::caution\n",
    "\n",
    "This is an unsafe feature because we currently do not model the lifetimes of\n",
    "capture-by-reference.\n",
    "\n",
    ":::"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<CommentService />"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Mojo",
   "language": "mojo",
   "name": "mojo-jupyter-kernel"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "mojo"
   },
   "file_extension": ".mojo",
   "mimetype": "text/x-mojo",
   "name": "mojo"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
